﻿using Newtonsoft.Json;
using System;
using System.Linq;
using T.FAS.Runtime.Metadata.Entity;
//using Newtonsoft.Json;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 数据查询-排序
    /// </summary>
    [Serializable]
    public class DataQueryColumn : ICloneable, IRefSchemaDetail
    {
        /// <summary>
        /// 主键id
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 数据查询id
        /// </summary>
        public string DataQueryID { get; set; }

        /// <summary>
        /// 数据对象id
        /// </summary>
        public string DataObjectID { get; set; }

        /// <summary>
        /// 数据对象id
        /// </summary>
        public string DataQuerySourceID { get; set; }

        /// <summary>
        /// 数据对象列id
        /// </summary>
        public string DataObjectColumnID { get; set; }

        /// <summary>
        /// 数据查询列名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 数据查询列别名
        /// </summary>
        public string Alias { get; set; }

        /// <summary>
        /// DQ展示列名
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// 列类型：0:普通列 1: 表达式列 2:GroupBy列 3:聚合函数列
        /// </summary>
        public ColumnType ColumnType { get; set; }

        /// <summary>
        /// 列头表达式，只有ColumnType: Express时生效
        /// </summary>
        public string ColumnExpression { get; set; }

        /// <summary>
        /// 列头表达式Json，只有ColumnType: Express时生效
        /// </summary>
        public string ColumnExpressionJson { get; set; }

        /// <summary>
        /// 列数据类型
        /// </summary>
        public string ColumnDataType { get; set; }

        /// <summary>
        /// 聚合类型，只有ColumnType: Aggregate时生效
        /// </summary>
        public AggregateType AggregateType { get; set; }

        /// <summary>
        /// 是否隐藏
        /// </summary>
        public bool IsHide { get; set; }

        /// <summary>
        /// 列头排序
        /// </summary>
        public int Sort { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext1 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext2 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext3 { get; set; }

        /// <summary>
        /// 数据对象实体，禁止序列化，防止引用错误
        /// </summary>
        [JsonIgnore]
        public DataObject DataObject { get; set; }

        /// <summary>
        /// 数据对象列实体，禁止序列化，防止引用错误
        /// </summary>
        [JsonIgnore]
        public DataColumn DataColumn { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        [JsonIgnore]
        public string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>        
        [JsonIgnore]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        [JsonIgnore]
        public string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>        
        [JsonIgnore]
        public DateTime LastModifyTime { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        public string GetRefSchemaExpr(IMetaDataContent metaData)
        {
            if (metaData is DataQuery dq)
            {
                var top = dq.GetTopRefSchemaExpr();
                if (ColumnType == ColumnType.Normal)
                {
                    DataQuerySource dataQuerySource = dq.DataQuerySources.FirstOrDefault(t => t.ID == DataQuerySourceID);
                    return $"{top}.{DataQuery.Sources}.{dataQuerySource.Alias}.{DataQuery.Columns}.{Alias}";
                }
                else if (ColumnType == ColumnType.Express)
                {
                    return $"{top}.{DataQuery.ExprColumnNameSpace}.{Alias}";
                }
                else
                {
                    return $"{top}.{DataQuery.AggAndGroupColumnNameSpace}.{Alias}";
                }
            }
            return null;
        }
    }

    /// <summary>
    /// DataQuery列数据类型
    /// </summary>
    public enum ColumnDataType:int
    {
        Text = 0,
        Numeric = 1,
        DateTime = 2,
        Blob = 3,
    }

    /// <summary>
    /// DataQuery列类型
    /// </summary>
    public enum ColumnType:int 
    {
        Normal = 0,
        Express = 1,
        Group = 2,
        Aggregate = 3,
    }

    /// <summary>
    /// 基本聚合函数类型
    /// </summary>
    public enum AggregateType : int
    {
        COUNT = 0,
        SUM = 1,
        AVG = 2,
        MAX = 3,
        MIN = 4
    }
}
